package variable

import "strings"

type SMap struct{}

func CreateVariableSMap() *SMap {
	return &SMap{}
}

// Set 插入数据
func (s *SMap) Set(key string, value interface{}) (res bool) {

	res = false
	if _, exists := s.KeyIsExists(key); exists == false {
		sMap.Store(key, value)
		res = true
	}

	return
}
func (s *SMap) Get(key string) interface{} {
	if value, exists := s.KeyIsExists(key); exists {
		return value
	}
	return nil
}

// KeyIsExists  判断键是否被注册
func (s *SMap) KeyIsExists(key string) (interface{}, bool) {
	return sMap.Load(key)
}

// Delete 删除
func (s *SMap) Delete(key string) {
	sMap.Delete(key)
}

// FuzzyDelete 按照键的前缀模糊删除容器中注册的内容
func (s *SMap) FuzzyDelete(keyPre string) {
	sMap.Range(func(key, value interface{}) bool {
		if rename, ok := key.(string); ok {
			if strings.HasPrefix(rename, keyPre) {
				sMap.Delete(rename)
			}
		}
		return true
	})
}
